using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.Mail;
using CodeSoft.Datos;
using CodeSoft.Logica;
using DataLayer;
using DataLayerUtilities;

namespace MailService
{
	/// <summary>
	/// Summary description for WebForm1.
	/// </summary>
	public class WebForm1 : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.TextBox txtFrom;
		protected System.Web.UI.WebControls.TextBox txtTo;
		protected System.Web.UI.WebControls.TextBox txtSubject;
		protected System.Web.UI.WebControls.Label Label1;
		protected System.Web.UI.WebControls.Label Label2;
		protected System.Web.UI.WebControls.Button btnSend;
		protected System.Web.UI.WebControls.Button btnCancel;
		protected System.Web.UI.WebControls.TextBox txtMailBody;
		protected System.Web.UI.WebControls.Literal litMsg;
		protected System.Web.UI.WebControls.Label Label4;
		protected System.Web.UI.HtmlControls.HtmlInputFile fileAdjunto;
		protected System.Web.UI.WebControls.Button Button1;
		protected System.Web.UI.WebControls.ListBox cboMiembros;
		protected System.Web.UI.WebControls.Label Label3;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			Server.ScriptTimeout = 30000000;
			
			if (! Page.IsPostBack)
			{
				cboMiembros.Items.Clear();
				LstMiembros lstM = new LstMiembros();
				lstM.CargarTodo();
				Miembro m;
				ListItem it;
				foreach (Registro reg in lstM)
				{
					m = new Miembro(reg);
					it = new ListItem(m.Nombre);
					it.Value = m.ID.ToString();
					cboMiembros.Items.Add(it);
				}
			}
			
			
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.Button1.Click += new System.EventHandler(this.Button1_Click);
			this.btnSend.Click += new System.EventHandler(this.btnSend_Click);
			this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void btnSend_Click(object sender, System.EventArgs e)
		{
			int cont = 0;
			
			foreach (ListItem it in cboMiembros.Items)
			{
				if (it.Selected)
				{
					Miembro m = new Miembro(int.Parse(it.Value));
					
					// Create message.
					MailMessage msgMail = new MailMessage();
			
					// Set message properties.
					msgMail.From = "CodesoftPV <" + txtFrom.Text +">";
					msgMail.BodyFormat = MailFormat.Html;
					msgMail.To = m.Email;
					msgMail.Subject = txtSubject.Text;
					msgMail.Body = txtMailBody.Text;
					msgMail.Body = msgMail.Body.Replace("<!--NOMBRE-->", m.Nombre);
					//msgMail.Body = msgMail.Body.Replace("<!--ENLACE-->",HttpUtility.UrlEncode("hercax.net/codesoftweb/Confirm.aspx?a="+EncryptionServices.Encrypt(m.ID.ToString()+"-2","codesoftpv")));
					if (fileAdjunto.Value != "")
					{
						// Create an attachment.
						MailAttachment atcItem = new MailAttachment(@fileAdjunto.PostedFile.FileName); 
						msgMail.Attachments.Add(atcItem);
					}
			
					msgMail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"] = 25;
					msgMail.Fields["http://schemas.microsoft.com/cdo/configuration/sendusing"]  = 2;
					msgMail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"] = 1;
					msgMail.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"] = "hercax@gmail.com";
					msgMail.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"] = "cghc9683";
					msgMail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpusessl"]= "true";

					// Send message.
					SmtpMail.SmtpServer = "smtp.gmail.com";
					
					try
					{
						SmtpMail.Send(msgMail);	
						m.FechaEnvioUltimoMail = DateTime.Now;
						m.Guardar();
					}
					catch (Exception ex)
					{
						Debug.WriteLine(ex.Message);
					}
					
					cont++;
				}
			}
			
			
			LimpiarText();
			litMsg.Text = cont.ToString() + " Mensaje(s) enviado satisfactoriamente.";
		}
		private void LimpiarText()
		{
			txtFrom.Text="";
			txtSubject.Text="";
			txtMailBody.Text="";
		}

		private void btnCancel_Click(object sender, System.EventArgs e)
		{
			LimpiarText();
			litMsg.Text = "";
		}

		private void Button1_Click(object sender, System.EventArgs e)
		{
			foreach (ListItem it in cboMiembros.Items)
			{
				it.Selected = true;
			}
		}

	}
}
